<?php if (!defined('THINK_PATH')) exit(); /*a:1:{s:61:"/www/jilly/public/../application/view/admin/user/account.html";i:1545723569;}*/ ?>
<!--<style>-->
    <!--.layui-btn{margin:3px;}-->
<!--</style>-->

<div style="margin-left: 94%;">
    <a class="layui-btn layui-btn-primary layui-btn-sm operate" operate-type="add"><i class="layui-icon">&#xe608;</i>添加</a>
</div>

<table class="layui-table">
    <thead>
    <tr>
        <th width="5%;">ID</th>
        <th width="10%;">昵称</th>
        <th width="10%;">邮箱</th>
        <th width="10%;">职位</th>
        <th width="5%;">创建日期</th>
        <th width="10%;">操作</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
</table>
<!--分页start-->
<div id="paginate"></div>
<!--分页end-->

<script>
    $(function(){
        GetTableList();
    });

    //获取表格数据
    function GetTableList(page=1,pagesize=10){
        var url = '/handler/user/getAccountList';
        var data = {page:page,pagesize:pagesize};
        dink.post(url,data,function(res){
            if(res.ret == 1){
                var target = $('tbody');
                var html = '';
                var tmp_root = 0;
                $.each(res.data,function(i,ele){
                    if(ele.role_name != '超级管理员'){
                        html += '<tr>\n' +
                            '    <td>'+(i+1)+'</td>\n' +
                            '    <td>'+ele.nickname+'</td>\n' +
                            '    <td>'+ele.email+'</td>\n' +
                            '    <td>'+(ele.role_id==0 ? 'root' : ele.role_name)+'</td>\n' +
                            '    <td>'+dink.utd(ele.ctime)+'</td>\n' +
                            '    <td>\n' +
                            '        <a class="layui-btn layui-btn-xs operate" operate-type="edit" data-id="'+ele.id+'">编辑</a>\n' +
                            '        <a class="layui-btn layui-btn-xs operate" operate-type="delete" data-id="'+ele.id+'">删除</a>\n' +
                            '    </td>\n' +
                            '</tr>';
                    }else{
                        tmp_root++;
                    }
                });
                if(html != ''){
                    if(page == 1){
                        Paginate(res.total - tmp_root,pagesize);
                    }
                }else{
                    html = '<tr><td colspan="6">无数据</td></tr>';
                }
                target.html(html);
            }else{
                var target = $('tbody');
                var html = '<tr><td colspan="6">无数据</td></tr>';
                target.html(html);
                layer.msg(res.msg);return;
            }
        });
    }

    //分页
    function Paginate(count,pagesize){
        layui.use('laypage',function(){
            var laypage = layui.laypage;
            laypage.render({
                elem: 'paginate',
                count: count,
                limit: pagesize,
                layout: ['count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj,first){
                    if(!first){
                        GetTableList(obj.curr,pagesize);
                    }
                }
            });
        });
    }

    //操作按钮click事件
    dink.click('.operate',function(){
        var operate_type = $(this).attr('operate-type');
        var id = $(this).attr('data-id');
        //判断权限
        dink.post('/handler/permission/checkOperatePermission',{permission:'user:account:'+operate_type},function(res){
            if(res.ret == 1){
                AdminUserAccountOpreate(operate_type,id);
            }else{
                layer.msg(res.msg);return;
            }
        });
        //end
    });

    //添加/编辑项目
    function AdminUserAccountOpreate(operate,id=0){
        if(operate == 'add'){
            dink.html('AdminUserAccountAdd','添加成员',['400px','460px']);
        }else if(operate == 'edit'){
            dink.html('AdminUserAccountAdd','编辑成员',['400px','360px']);
            setTimeout(function(){
                dink.post('/handler/user/getAccountDetailById',{id:id},function(res){
                    var item = res.data;
                    $('input[name=nickname]').val(item.nickname);
                    $('input[name=password]').val(item.password);
                    $('input[name=password_confirm]').val(item.password);
                    $('input[name=email]').val(item.email);
                    $('input[name=mobile]').val(item.mobile);
                    $('input[name=password]').parent().parent().hide();
                    $('input[name=password_confirm]').parent().parent().hide();
                    setTimeout(function(){
                        $('select[name=position]').val(item.role_id);
                    },50);
                });
            },50);
        }else if(operate == 'delete'){
            dink.alert('Tips','确定删除？',function(){
                dink.post('/handler/user/deleteAccount',{id:id},function(res){
                    layer.msg(res.msg);
                    GetTableList();return;
                });
            });
        }else{
            layer.msg('操作类型异常');return;
        }
        //添加、编辑
        dink.click('#add',function(){
            var data = {};
            var base = {};
            if(operate == 'add'){
                var url = '/handler/user/addAccount';
            }else if(operate == 'edit'){
                var url = '/handler/user/editAccount';
                if(id == 0){
                    layer.msg('缺少记录id');return;
                }
                data.id = id;
            }else{
                layer.msg('操作类型异常');return;
            }
            base.nickname = $('input[name=nickname]').val();
            base.password = $('input[name=password]').val();
            base.email = $('input[name=email]').val();
            base.mobile = $('input[name=mobile]').val();
            base.role_id = $('select[name=position]').val();
            data.base = base;
            if(base.nickname == ''){
                layer.msg('请填写成员昵称');return;
            }
            if(base.password == ''){
                layer.msg('请填写密码');return;
            }
            if(base.password != $('input[name=password_confirm]').val()){
                layer.msg('两次密码输入不一致');return;
            }
            if(base.email == ''){
                layer.msg('请填写邮箱');return;
            }
            if(base.mobile == ''){
                layer.msg('请填写手机号');return;
            }
            if(base.role_id == 0){
                layer.msg('请选择职位');return;
            }
            var preg_email = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
            if(!preg_email.test(base.email)){
                layer.msg('邮箱格式不正确');return;
            }
            var preg_mobile = /^1[34578]\d{9}$/;
            if(!preg_mobile.test(base.mobile)){
                layer.msg('手机格式不正确');return;
            }
            dink.alert('Tips','确定'+(operate=='add' ? '添加' : '编辑')+'成员？',function(){
                dink.post(url,data,function(res){
                    if(res.ret == 1){
                        dink.close(res.msg);
                        GetTableList();
                    }else{
                        layer.msg(res.msg);
                    }
                });
            });
        });
    }
</script>